package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbOrderMapper;
import com.bwie.param.CouponCodeParam;
import com.bwie.pojo.TbCouponCode;
import com.bwie.pojo.TbLog;
import com.bwie.pojo.TbOrder;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbCouponCodeService;
import com.bwie.mapper.TbCouponCodeMapper;
import com.bwie.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author HUAWEI
* @description 针对表【tb_coupon_code】的数据库操作Service实现
* @createDate 2025-05-19 09:50:50
*/
@Service
public class TbCouponCodeServiceImpl extends ServiceImpl<TbCouponCodeMapper, TbCouponCode>
    implements TbCouponCodeService{
   @Autowired
   private TbCouponCodeMapper tbCouponCodeMapper;
   @Autowired
    StringRedisTemplate stringRedisTemplate;

   @Autowired
   private TbOrderMapper tbOrderMapper;
    @Override
    public R getCouponList(CouponCodeParam couponCodeParam) {
        Page<TbCouponCode> page = new Page<>(couponCodeParam.getPageNum(), couponCodeParam.getPageSize());
        LambdaQueryWrapper<TbCouponCode> wrapper = new LambdaQueryWrapper<>();
        //名称查询
        if(!StringUtils.isBlank(couponCodeParam.getCouponCodeName())){
            wrapper.eq(TbCouponCode::getCouponCodeName,couponCodeParam.getCouponCodeName());
        }
        Page<TbCouponCode> tbCouponCodePage = tbCouponCodeMapper.selectPage(page, wrapper);
        return R.OK(tbCouponCodePage);
    }

    @Override
    public R toupdateNum(Integer couponCodeId) {
        //修改劵码数量
        tbCouponCodeMapper.toupdateNum(couponCodeId);
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOrderStatus("寄售中");
        tbOrder.setCouponCodeId(1);
        tbOrder.setTradeTime(new Date());
        tbOrder.setUserId(2);
        tbOrder.setPrice(10.00);
        tbOrder.setOrderNum(10);
        tbOrderMapper.insert(tbOrder);
        return R.OK();
    }
    @PostConstruct
    public void init(){
        //查询订单列表
        List<TbCouponCode> list = list();
        //存入redis缓存
        stringRedisTemplate.opsForList().rightPushAll("couponCodeList", JSON.toJSONString(list));

    }

}




